using System;
using Project_Flight;

namespace Project_Flight.intrnl
{
    public class SynchronizationGroup  : Sybase.Persistence.ISynchronizationGroup
    {        
        private SISSubscription __sisSubscription;

        private string GetRemoteId()
        {
            foreach (Project_Flight.KeyGenerator kg in Project_Flight.KeyGenerator.FindAll())
            {
                if (!kg.RemoteId.Equals(".")) return kg.RemoteId;
            }
            return "";
        }
        
        private SynchronizationGroup(String ___name)
        {     
            SISSubscriptionKey _key = new SISSubscriptionKey();
            _key.ClientId = GetRemoteId();
            string domainName = FlightDB.GetSynchronizationProfile().DomainName;
            if(domainName == null || domainName.Length == 0)
            {
                domainName = Sybase.Mobile.Application.GetInstance().ApplicationSettings.GetStringProperty(Sybase.Mobile.ConnectionPropertyType.DomainName);
            }
            if(domainName == null || domainName.Length == 0)
            {
                domainName = "default";
            }
            _key.Domain = domainName;
            _key.Package = FlightDB.GetPackageName();
            _key.SyncGroup = ___name;
            __sisSubscription = SISSubscription.Find(_key);
            if(__sisSubscription == null)
            {
                __sisSubscription = new SISSubscription();
                __sisSubscription.Domain = _key.Domain;
                __sisSubscription.Package = _key.Package;
                __sisSubscription.SyncGroup = _key.SyncGroup;
                __sisSubscription.ClientId = _key.ClientId;
                __sisSubscription.Username = FlightDB.GetSyncUsername();
                __sisSubscription.DeviceId = com.sybase.afx.util.PidUtil.GetId();
            }
        }

        public static SynchronizationGroup GetInstance(string ___name)
        {
            return new SynchronizationGroup(___name);
        }
                

        public string Name
        {
            get
            {
                return __sisSubscription.SyncGroup;
            }
        }

        public bool AdminLock
        {
            get
            {
                return __sisSubscription.AdminLock;
            }
        }

        public bool EnableSIS
        {
            get
            {
                return __sisSubscription.Enable;
            }
            set
            {
                __sisSubscription.Enable = value;
            }
        }

        public int Interval
        {
            get
            {
                return __sisSubscription.Interval;
            }
            set
            {
                __sisSubscription.Interval = value;
            }
        }

        public  void Save()
        {
            __sisSubscription.Protocol = FlightDB.GetSynchronizationProfile().GetProperty("protocol", "TCN");
            __sisSubscription.Address = FlightDB.GetSynchronizationProfile().GetProperty("address");
            __sisSubscription.Appname = FlightDB.GetSynchronizationProfile().GetProperty("appname", "sup");
            __sisSubscription.Save();
        }
    }
}